package com.mo.demo.ehcache;

import lombok.extern.slf4j.Slf4j;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.*;

@Slf4j
public class EhcacheSerializerDemo {
    public static void main(String[] args) throws InterruptedException {
        CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                .withCache("preConfigured", CacheConfigurationBuilder.newCacheConfigurationBuilder(Number.class, String.class, ResourcePoolsBuilder.heap(10))
                        .withExpiry(new MyExpiryPolicy<>())
                        .build())
                .withSerializer(Number.class, new EhcacheSerializer<Number>().getItsClass())
                .withSerializer(String.class, new EhcacheSerializer<String>().getItsClass())
                .withCopier(Number.class,new EhcacheCopier<Number>().getItsClass())
                .withCopier(String.class,new EhcacheCopier<String>().getItsClass())
                .build(true);


        Cache<Number, String> myCache = cacheManager.getCache("preConfigured", Number.class, String.class);

        myCache.put(1,"1111");

        log.info("value={}",myCache.get(1));

        // 输出：
        // 12:32:00.245 [main] DEBUG org.ehcache.core.spi.ServiceLocator - Starting 16 Services...
        // 12:32:00.260 [main] DEBUG org.ehcache.core.spi.ServiceLocator - All Services successfully started, 16 Services in 15ms
        // 12:32:00.260 [main] DEBUG org.ehcache.core.EhcacheManager - Creating Cache 'preConfigured' in EhcacheManager.
        // 12:32:00.260 [main] DEBUG org.ehcache.impl.internal.spi.serialization.DefaultSerializationProvider - Serializer for <java.lang.Number> : com.mo.demo.ehcache.EhcacheSerializer@581ac8a8
        // 12:32:00.260 [main] DEBUG org.ehcache.impl.internal.spi.serialization.DefaultSerializationProvider - Serializer for <java.lang.String> : com.mo.demo.ehcache.EhcacheSerializer@505fc5a4
        // 12:32:00.276 [main] DEBUG org.ehcache.impl.internal.spi.copy.DefaultCopyProvider - Copier for <java.lang.Number> : com.mo.demo.ehcache.EhcacheCopier@bae7dc0
        // 12:32:00.276 [main] DEBUG org.ehcache.impl.internal.spi.copy.DefaultCopyProvider - Copier for <java.lang.String> : com.mo.demo.ehcache.EhcacheCopier@209da20d
        // 12:32:00.291 [main] DEBUG org.ehcache.impl.internal.store.heap.OnHeapStore - TTI or custom expiration strategy detected
        // 12:32:00.338 [main] DEBUG class org.ehcache.core.Ehcache-preConfigured - Initialize successful.
        // 12:32:00.338 [main] INFO org.ehcache.core.EhcacheManager - Cache 'preConfigured' created in EhcacheManager.
        // 12:32:00.338 [main] DEBUG org.ehcache.core.EhcacheManager - Initialize successful.
        // 12:32:00.385 [main] INFO com.mo.demo.ehcache.EhcacheSerializerDemo - value=1111
    }

}
